package com.example.demo.design;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class StrategyContext {
    /**
     *使用线程安全的ConcurrentHashMap存储所有实现Strategy接口的Bean
     *key:beanName
     *value：实现Strategy接口Bean
     */
    private final Map<String, Strategy> strategyMap = new ConcurrentHashMap<>();

    /**
     * 注入所有实现了Strategy接口的Bean
     * @param strategyMap
     */
    @Autowired
    public StrategyContext(Map<String, Strategy> strategyMap) {
        this.strategyMap.clear();
        /**
         * 这是JDK1.8的实现
         */
//        strategyMap.forEach((k, v)-> this.strategyMap.put(k, v));

        /**
         * 则是jdk1.7的实现
         */
        for (Map.Entry<String, Strategy> entry : strategyMap.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            this.strategyMap.put(entry.getKey(),entry.getValue());
        }

    }

    /**
     * 计算价格
     * @param memberLevel   会员等级
     * @return              价格
     */
    public BigDecimal calculatePrice(String memberLevel) {
        if(!StringUtils.isEmpty(memberLevel)){
            return strategyMap.get(memberLevel).calculatePrice();
        }
        return null;
    }

}
